package beyjava.daoImp;

import java.util.List;
import java.util.Map;
import java.util.Vector;
import beyjava.dao.ActivityDAO;
import beyjava.dao.ProjectDAO;
import beyjava.dbUtil.DbUtility;
import beyjava.model.Activity;
import beyjava.util.Common;

/**
 * ActivityDao implementation
 * @author BEYJAVA
 *
 */
public class ActivityDAOImp implements ActivityDAO {

	public boolean insertActivity(Activity newActivity) {
		StringBuffer query=new StringBuffer();
		query.append(" INSERT INTO ");
		query.append(" activity ");
		query.append(" ( actNumber,actDesc,actStartDate,actDeliver,hours,projectID ) ");
		query.append(" VALUES ( ?,?,?,?,?,? )");
		
		Object []params=new Object[]{newActivity.getActNumber(),newActivity.getActDesc()
				,Common.convertDateToStringFormat(newActivity.getActStartDate(),"yyyy-MM-dd")
				, newActivity.getActDeliver(),newActivity.getHours(),newActivity.getActivityProject().getProjectID()
		                            
		};
		DbUtility db=DbUtility.getInstance();
		int success = db.update(query.toString(), params);
		if (success < 1) {
			return false; 
		}
		return true;
	}
	public boolean removeActivity(int activityID) {
		// TODO Auto-generated method stub
		StringBuffer query=new StringBuffer();
		query.append("DELETE FROM activity ");
		query.append(" Where actNumber =? ");
		
		Object []params=new Object[]{activityID};
		DbUtility db = DbUtility.getInstance();
		int success = db.update(query.toString(),params);
	    if(success < 1){
	    	return false;
	    }
	    return true;
	}
	public boolean updateActivity(Activity updatedActivity) {
		// TODO Auto-generated method stub
		StringBuffer query= new StringBuffer();
		query.append(" UPDATE activity ");
		query.append(" SET actDesc=? , actStartDate=? , actDeliver=? , hours=? ");
		query.append(" WHERE actNumber = ?");
		Object []params = new Object[]{updatedActivity.getActDesc()
				,Common.convertDateToStringFormat(updatedActivity.getActStartDate(),"yyyy-MM-dd")
					     ,updatedActivity.getActDeliver(),updatedActivity.getHours(),updatedActivity.getActNumber()					
									};
		DbUtility db = DbUtility.getInstance();
		int success = db.update(query.toString(), params);
		if(success < 1){
			return false;
		}
		return true;
	}
	public Vector<Activity> getActivitiesOfProject(int projectID) {
		StringBuffer query = new StringBuffer();
		query.append(" SELECT * FROM activity ");
		query.append(" WHERE projectID = ? ");
		Object []params = new Object[]{projectID};
        DbUtility db = DbUtility.getInstance();
        List<Map<String,Object>> activityRows = db.execute(query.toString(),params);
        if(activityRows == null || activityRows.size() < 1){
        	return null;
         }
        Vector<Activity> activities = new Vector<Activity>();
        for(Map<String,Object> activityRow :activityRows){
        	Activity newActivity = convertAcvitiyRowToActivity(activityRow);
        	activities.add(newActivity);
        }
		return activities;
	}
	public Activity getActivity(int activityID) {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer();
		query.append(" SELECT * FROM activity ");
		query.append(" WHERE actNumber=? ");
		Object []params = new Object[]{activityID};
        DbUtility db = DbUtility.getInstance();
        List<Map<String,Object>> activityRows = db.execute(query.toString(),params);
        if(activityRows == null || activityRows.size() < 1){
        	return null;
         }
        Map<String,Object> activityRow = activityRows.get(0);
        return convertAcvitiyRowToActivity(activityRow);
	}
	/**
	 * This method provide that a activity object is created from a row that describe 
	 * the activity in database( or table )
	 * @param activityRow
	 * @return Activity Object
	 */
	private Activity convertAcvitiyRowToActivity(Map<String,Object> activityRow){
		 Integer actNumber = (Integer)activityRow.get("actNumber");
		 String actDesc = activityRow.get("actDesc").toString();
		 String actDate = activityRow.get("actStartDate").toString();
		 Integer actHours = (Integer)activityRow.get("hours");
		 String actDeliver = activityRow.get("actDeliver").toString();
		 Integer projectID = (Integer)activityRow.get("projectID");
		 ProjectDAO projectDAO = new ProjectDAOImp();
		 Activity newActivity = new Activity(actNumber, actDesc, Common.convertToDate(actDate,"yyyy-MM-dd"), actDeliver,actHours,
				projectDAO.selectProjectByID(projectID));
        return newActivity;
	}

}
